package com.itcast.service.impl;

import com.itcast.mapper.DeptMapper;
import com.itcast.pojo.Dept;
import com.itcast.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

//@Primary  优先执行该实现类
@Service
@Transactional
public class DeptServiceImpl implements DeptService {
    /*@Autowired
    private DeptDao deptDao;*/
    @Autowired
    private DeptMapper deptMapper;

    /**
     * 部门列表
     * @return
     * @throws IOException
     */
    public List<Dept> handleData() throws IOException {
        List<Dept> deptList = deptMapper.findAll();
        return deptList;
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    public void delete(Integer id) {
        deptMapper.delete(id);
    }

    /**
     * 新增部门
     * @param dept
     * @return
     */
    public void add(Dept dept) {
        //1.补全基础属性(也可以在sql语句中补全-->sql语句是写死的，必定会调用)
//        dept.setCreateTime(LocalDateTime.now());
//        dept.setUpdateTime(LocalDateTime.now());
        //2.调用mapper接口方法
        deptMapper.add(dept);
    }

    /**
     * 根据id查找
     * @param id
     * @return
     */
    public Dept getById(Integer id) {
        return deptMapper.getById(id);
    }

    /**
     * 修改部门
     * @param dept
     * @return
     */
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }

}
